package user

import (
	"encoding/json"

	"gitee.com/liuxiaowei123/vblog-1.1/common"
	"golang.org/x/crypto/bcrypt"
)

type User struct {
	*common.Meta
	*CreaterUserRequest
}

func (u *User) String() string {
	js, _ := json.Marshal(u)
	return string(js)
}

func NewUser(req *CreaterUserRequest) *User {
	req.PasswordHash()
	return &User{
		Meta:               common.NewMeta(),
		CreaterUserRequest: req,
	}
}

func (u *User) CompareHash(password string) error {
	err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
	if err != nil {
		return err
	}
	return nil
}
